﻿#include "login.h"
#include "registrationpage.h"
#include "ui_login.h"

#include<QDebug>
#include<QFile>
#include<QMessageBox>
#include<QDateTime>

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{

    ui->setupUi(this);
    //Login *login = new Login();
    this->setWindowTitle("登录");
    this->setFixedSize(500,230);

    //MainWindow *
    mainWin = new MainWindow();
    registrationPage *regLogin = new registrationPage();

    connect(regLogin,&registrationPage::back,[=](){
        regLogin->hide();
        this->show();

    });


    connect(ui->pushButtonLogin,&QPushButton::clicked,[=](){//登录

        QString use,pas;
        //获取用户名数据库
        QFile file("user.txt");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug()<<"D:/user.txt";
            return;
        }
        while (!file.atEnd()) {
            use = file.readLine();
        }

        //获取密码数据库
        QFile file1("D:/password.txt");
        if (!file1.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        /*
        qDebug()<<"user读到的文件内容"<<use;
        qDebug()<<"password读到的文件内容"<<pas;
        */
        //用户名密码核对

        QString us = ui->lineEditUser->text().toUtf8().data();
        QString pa = ui->lineEditPassword->text().toUtf8().data();

        /*
        qDebug()<<"user读到输入内容"<<us;
        qDebug()<<"password读到输入内容"<<pa;
        */
        if(use.contains(us,Qt::CaseSensitive) && pas.contains(pa,Qt::CaseSensitive) && us!=NULL && pa != NULL)
        {
            this->hide();
            mainWin->show();
            //emit lan->xinhao();
            //myitme<<"登录成功";

        }else
        {
            QMessageBox::critical(this,"警告","密码或用户名错误");
            //emit lan->xinhao();
            //myitme<<"登录失败";

        }
    });
    connect(ui->pushButtonEnroll,&QPushButton::clicked,[=](){//注册按键
        //myitme<<"注册";
        //emit lan->xinhao();
        this->hide();
        regLogin->show();

    });
    connect(ui->pushButtonQuit,&QPushButton::clicked,[=](){//退出按键
        qDebug()<<"退出";
        this->close();
    });
}

Login::~Login()
{
    delete ui;
}

void Login::on_lineEditPassword_editingFinished()
{
    //MainWindow *mainWin = new MainWindow();
    QString use,pas;
    //获取用户名数据库
    QFile file("D:/user.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<"D:/user.txt";
        return;
    }
    while (!file.atEnd()) {
        use = file.readLine();
    }

    //获取密码数据库
    QFile file1("D:/password.txt");
    if (!file1.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<"数据库读取失败";
        return;
    }
    while (!file1.atEnd()) {
        pas = file1.readLine();
    }
    /*
    qDebug()<<"user读到的文件内容"<<use;
    qDebug()<<"password读到的文件内容"<<pas;
    */
    //用户名密码核对

    QString us = ui->lineEditUser->text().toUtf8().data();
    QString pa = ui->lineEditPassword->text().toUtf8().data();

    /*
    qDebug()<<"user读到输入内容"<<us;
    qDebug()<<"password读到输入内容"<<pa;
    */
    if(use.contains(us,Qt::CaseSensitive) && pas.contains(pa,Qt::CaseSensitive) && us!=NULL && pa != NULL)
    {
        this->hide();
        mainWin->show();
        //emit lan->xinhao();
        //myitme<<"登录成功";

        QDateTime time = QDateTime::currentDateTime();

        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"

        QString pas;
        QFile file1("D:/mylog.txt");
        if (!file1.open(QIODevice::Append | QIODevice::Text))
        {
            qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pas="登录成功";
        QByteArray  strBytes=pas.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        pas="\n"+mytime+"\n";
        QByteArray  strBytes1=pas.toUtf8();//转换为字节数组
        file1.write(strBytes1,strBytes1.length());  //写入文件
        //qDebug()<<"修改后"<<pas;
        file1.close();
    }else
    {
        QMessageBox::critical(this,"警告","密码或用户名错误");
        //emit lan->xinhao();
        //myitme<<"登录失败";

        QDateTime time = QDateTime::currentDateTime();

        QString mytime = time.toString("yyyy-MM-dd hh:mm:ss");//"yyyy-MM-dd hh:mm:ss dddd"


        QString pas;
        QFile file1("D:/mylog.txt");
        if (!file1.open(QIODevice::Append | QIODevice::Text))
        {
            //qDebug()<<"数据库读取失败";
            return;
        }
        while (!file1.atEnd()) {
            pas = file1.readLine();
        }
        //qDebug()<<"密码修改前"<<pas;
        pas="登录失败";
        QByteArray  strBytes=pas.toUtf8();//转换为字节数组
        file1.write(strBytes,strBytes.length());  //写入文件
        pas="\n"+mytime+"\n";
        QByteArray  strBytes1=pas.toUtf8();//转换为字节数组
        file1.write(strBytes1,strBytes1.length());  //写入文件
        //qDebug()<<"密码修改后"<<pas;
        file1.close();

    }
}

void Login::on_pushButtonLogin_2_clicked()
{
    ui->lineEditUser->setText("");
    ui->lineEditPassword->setText("");
}
